package org.pcorp.battlespace.construction.ui.tools;

import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.text.ParseException;

import javax.swing.BorderFactory;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.text.NumberFormatter;

public class DialogUi extends JDialog {
	private static final long serialVersionUID = -913442605825581021L;
	private static final NumberFormatter nform = new NumberFormatter();
	
	public DialogUi() {
	
	}
	
	public DialogUi(Frame frame, boolean bool) {
		super(frame, bool);
	}
	
	protected GridBagConstraints initLayout() {
		setLayout(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints();
		
		gbc.gridx = gbc.gridy = 0;
	    gbc.gridwidth = gbc.gridheight = 1;
	    gbc.weightx = gbc.weighty = 0;
	    gbc.fill = GridBagConstraints.HORIZONTAL;
	    
	    return gbc;
	}
	
	public long getLong(JTextField j) {
		if (j.getText()!=null && !j.getText().equalsIgnoreCase("")) {
			return Long.parseLong(j.getText().replaceAll("'", ""));
		}
		else {
			return 0;
		}
	}
	
	protected JPanel getPanel() {
		return new JPanel();
	}
	
	public JLabel getLabel(String label) {
		JLabel jl = new JLabel(label);
		initLabel(jl);
		return jl;
	}
	
	public void initLabel(JLabel label) {
		label.setPreferredSize(new Dimension(ConstanteUI.LABELW, ConstanteUI.LABELH));
		label.setHorizontalAlignment(SwingConstants.RIGHT);
	}
	
	public JLabel getTextFixe(String label) {
		JLabel jl = new JLabel(label);
		jl.setPreferredSize(new Dimension(ConstanteUI.LABELW, ConstanteUI.LABELH));
		jl.setHorizontalAlignment(SwingConstants.LEFT);
		jl.setBorder(BorderFactory.createCompoundBorder(
						jl.getBorder(), 
				        BorderFactory.createEmptyBorder(0, 5, 0, 5)));
		return jl;
	}
	
	public JLabel getTextFixe(String label, int largeur) {
		JLabel jl = new JLabel(label);
		jl.setPreferredSize(new Dimension(largeur, ConstanteUI.LABELH));
		jl.setHorizontalAlignment(SwingConstants.LEFT);
		jl.setBorder(BorderFactory.createCompoundBorder(
						jl.getBorder(), 
				        BorderFactory.createEmptyBorder(0, 5, 0, 5)));
		return jl;
	}
	
	public JLabel getTextFixe(String label, boolean largeur) {
		if (largeur == true)
			return getTextFixe(label);
		
		JLabel jl = new JLabel(label);
		jl.setHorizontalAlignment(SwingConstants.LEFT);
		jl.setBorder(BorderFactory.createCompoundBorder(
						jl.getBorder(), 
				        BorderFactory.createEmptyBorder(0, 5, 0, 5)));
		return jl;
	}
	
	public JLabel getTextFixe(long label) {
		return getTextFixe(getLongText(label));
	}
	
	public JLabel getTextFixe(float label) {
		return getTextFixe(getFloatText(label));
	}
	
	public String getFloatText(float valeur) {
		try {
			return nform.valueToString(valeur);
		}
		catch (ParseException e) {
			return "0";
		}
	}
	
	public String getLongText(long valeur) {
		try {
			return nform.valueToString(valeur);
		}
		catch (ParseException e) {
			return "0";
		}
	}
	
	public JTextField getJTextField() {
		JTextField jtext = new JTextField();
		initJText(jtext);
		return jtext;
	}
	
	public void initJText(JTextField jtext) {
		jtext.setPreferredSize(new Dimension(ConstanteUI.JTEXTW, ConstanteUI.JTEXTH));
		jtext.setBorder(BorderFactory.createCompoundBorder(
						jtext.getBorder(), 
				        BorderFactory.createEmptyBorder(0, 5, 0, 5)));

	}
}
